<template>
  <div ref="operate_hab" class="operate-hab" style=" width: 100%;
  height: 53%;"></div>
</template>

<script>
import * as echarts from "echarts";
import { mapState } from "vuex";
export default {
  data() {
    return {
      chartInstance: null,
    };
  },
  computed: {
    ...mapState(["theme"]),
  },
  methods: {
    initChart() {
      // 初始化对象
      this.chartInstance = echarts.init(this.$refs.operate_hab, this.theme);
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {
          bottom: "1%",

          icon: "circle",
          itemGap: 6,

          textStyle: {
            color: "rgba(255,255,255,0.8)",
          },
          // 需要设置legend大小
        },
        grid: {
          left: "3%",
          right: "0%",
          bottom: "12%",
          top: "14%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: [
              "01-12",
              "01-13",
              "01-14",
              "01-15",
              "01-16",
              "01-17",
              "01-18",
            ],
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: true, color: "rgba(255,255,255,0.5)",
  
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              lineStyle: {
                color: "#1a1f6d",
                width: 1,
              },
            },
            axisLabel: {
              show: true, color: "rgba(255,255,255,0.5)",
  
            },
          },
        ],
        series: [
          {
            symbol: "circle",
            name: "打印",
            type: "bar",
            barWidth: 20,
            stack: "Search Engine",
            emphasis: {
              focus: "series",
            },
            data: [620, 732, 701, 734, 1090, 1130, 1120],
          },
          {
            name: "刻录",
            type: "bar",
            stack: "Search Engine",
            emphasis: {
              focus: "series",
            },
            data: [120, 132, 101, 134, 290, 230, 220],
          },
          {
            name: "打印贡献",
            type: "bar",
            stack: "Search Engine",
            emphasis: {
              focus: "series",
            },
            data: [60, 72, 71, 74, 190, 130, 110],
          },
          {
            name: "刻录大小",
            type: "bar",
            stack: "Search Engine",
            emphasis: {
              focus: "series",
            },
            data: [62, 82, 91, 84, 109, 110, 120],
          },
          {
            name: "共享传播",
            type: "bar",
            stack: "Search Engine",
            emphasis: {
              focus: "series",
            },
            data: [62, 82, 91, 84, 109, 110, 120],
          },
        ],
      };
      this.chartInstance.setOption(option);
    },
  },
  mounted() {
    this.initChart();
        // 添加随页面变化而变化
    window.addEventListener('resize', () => {
       this.chartInstance.resize()
    })
  },
      destroyed () {
    window.removeEventListener("resize", () => {
      this.chartInstance.resize();
    });
  },
  watch: {
    theme(newValue, oldValue) {
      this.chartInstance.dispose();
      this.initChart();
    },
  },
};
</script>

<style lang="scss" scoped>
.operate-hab {
  width: 100%;
  height: 53%;
}
</style>